package com.df.excel_export.common;

import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
public class ExportConfig {



    public String tablePrefix = "TB_";
    public String tableSuffix = "";
    public boolean containPrefix = true;
    public boolean containSuffix = false;
    public String scriptType = "none";

    public boolean exportAll = false;
    public String jsEngine = "Nashorn";

    private Map<String, Object> configs = new HashMap<>();


    //key tag名，value tag对象
    public Map<String,Tag> tags = new HashMap<>();

    public Map<String, Object> getConfigs(){
        return configs;
    }

    public Object getConfig(String key){
        return   configs.get(key);
    }
    public String getTagConfigKey(String tag,String suffix){
        return   Constant.config.Key_TagPrefix+tag+suffix;
    }
    public String getExportFiles(){
        return (String) configs.getOrDefault(Constant.config.KEY_ExportFiles,"");
    }
    public String getJsEngine(){
        return (String) configs.getOrDefault(Constant.config.KEY_JsEngine,jsEngine);
    }
    public String getExcelFilePath(){
        return (String) configs.getOrDefault(Constant.config.KEY_excelFilePath,"");
    }
    public int getDataStartRow(){
        return (int) configs.getOrDefault(Constant.config.KEY_DataStartRow,0);
    }
    public String getTablePrefix(){
        return (String) configs.getOrDefault(Constant.config.KEY_TablePrefix,tablePrefix);
    }
    public String getTableSuffix(){
        return (String) configs.getOrDefault(Constant.config.KEY_TableSuffix,tableSuffix);
    }
    public String getLocationPath(){
        return (String) configs.getOrDefault(Constant.config.Location_Prefix,"");
    }
    public String getScriptType(){
        return (String) configs.getOrDefault(Constant.config.KEY_ScriptType,scriptType);
    }
    public String getScriptLocation(){
        return (String) configs.getOrDefault(Constant.config.KEY_ScriptLocation,"");
    }

    public Boolean getClassNameContainPrefix(){
        return (Boolean) configs.getOrDefault(Constant.config.KEY_ClassNameContainPrefix,containPrefix);
    }
    public boolean getClassNameContainSuffix(){
        return (Boolean) configs.getOrDefault(Constant.config.KEY_ClassNameContainSuffix,containSuffix);
    }
    //获取所有Tag，按Tag进行代码生成
    public Tag getTag(String tagName){
        return tags.get(tagName);
    }
    public List<Tag> getTags(){
        return new ArrayList<>(tags.values());
    }
    public void setConfig(String key, Object value) {
        configs.put(key,value);
    }

    public void parserTags() {
        //解析tag名
        for (String s : configs.keySet()) {
            if(!s.startsWith("tag_")){
                continue;
            }
            String[] split = s.split("_");
            if(split.length != 2){
                continue;
            }
            String tagName = split[1];
            Tag tag = new Tag();
            tag.name = tagName;
            tags.put(tagName,tag);
        }
        List<String> remove = new ArrayList<>();
        for (String s : tags.keySet()) {
            Tag tag = tags.get(s);
            Object rowObj = configs.get(tag.getDataTypeDefineRowKey());
            if(rowObj == null || ((String)rowObj).isEmpty()){
                log.error("tag:{} 数据类型定义行未配置,移除此tag",tag.name);
                remove.add(tag.name);
                continue;
            }
            try {
                tag.row = Integer.parseInt((String)rowObj);
            }catch (Exception e){
                log.error("tag:{} 数据类型定义行配置错误,移除此tag",tag.name);
                remove.add(tag.name);
                continue;
            }
        }
        for (String s : remove) {
            tags.remove(s);
        }
  }
}
